package org.example.design.designmode.singleton;

import lombok.Data;

/**
 * @Auther: lingshi
 * @Date: 2020/5/10 18:14
 * @Description: 通过匿名内部类创建单例
 * 类加载 ：staticInteriorSingleton.class
 * 当程序有调用getInstance时他发现类下面有个匿名内部类就去加载staticInteriorSingleton$staticInterior.class
 * 优点：避免内存开销、效率问题，$不被反射进行破坏
 * 缺点 ：$如果在私有构造中没有判断匿名内部类是否为空，是会被class反射进行破坏的
 */
@Data
public class StaticInteriorSingleton {

    private  String name;
    private StaticInteriorSingleton() {
        if (StaticInterior.INTER != null) {
            throw new RuntimeException("禁止非法访问！");
        }
    }

    public static StaticInteriorSingleton getInstance() {
        return StaticInterior.INTER;
    }

    private static class StaticInterior {

        private static final StaticInteriorSingleton INTER = new StaticInteriorSingleton();

    }


}
